home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / Q-R / RIFF File Format / RIFFinput.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-15  |  3.9 KB  |  159 lines  |  [TEXT/KAHL]

  1. /*
  2. MOD 07-12-87 MAZ - RIFF input routines
  3. */
  4. #include <FileMgr.h>
  5. #include "MAZlib.h"
  6. #include "RIFF.h"
  7.  
  8. extern bool init_io();
  9. extern long init_read();
  10.  
  11. /* stdfile externs */
  12. extern long filter_type;
  13. extern long filter_creator;
  14. extern short global_volrefnum;
  15. extern bool input_file_name();
  16.  
  17. /* information as read in */
  18. unsigned char *body[4];
  19. short nrows;
  20. short ncols;
  21. short storage_type;
  22. short nsamples;
  23. short dpi;
  24. bool uncompressed;
  25. unsigned char *transfer_table;
  26.  
  27. /* from RIFFcompress.C */
  28. extern bool COMPinput();
  29.  
  30. /* open a new picture */
  31. /* note: if the nsamples of the picture is not equal to the nsamples desired */
  32. /*         conversion occurs */
  33. short open_RIFF(filename, desired_nsamples)
  34. char *filename; /* returned */
  35. short desired_nsamples; /* range: 2 to 256 */
  36.     {
  37.     bool b;
  38.     short flat, i, j, nchunks;
  39.     long lsize;
  40.     short *huffbuff;
  41.     ioParam readparm;
  42.     RIFF_header Rheader;
  43.  
  44.     /* get file for input */
  45.     filter_creator = 0;
  46.     filter_type = 'RIFF';    /* allow riff files only */
  47.     b = input_file_name("Open Picture:", filename);
  48.     if (!b)
  49.        return(-1); /* user cancelled */
  50.     /* open the file */
  51.     CtoPstr(filename);
  52.     readparm.ioNamePtr = (StringPtr)filename;
  53.     readparm.ioVersNum = 0;
  54.     readparm.ioVRefNum = global_volrefnum;
  55.     readparm.ioPermssn = fsRdPerm;
  56.     readparm.ioMisc = null;
  57.     PBOpen(&readparm, false);
  58.     PtoCstr(filename);
  59.     /* get out if the open failed (this should not happen) */
  60.     if (readparm.ioResult != 0)
  61.         return(2); /* coudn't open file */
  62.     /* read the pattern record from the file */
  63.     if (!init_io(2048L))
  64.         goto no_mem2;
  65.     init_read(&readparm, 0L);
  66.     getbytes(&readparm, &Rheader, (long)sizeof(RIFF_header));
  67.     nrows = Rheader.nrows;
  68.     ncols = Rheader.ncols;
  69.     nsamples = Rheader.nsamples;
  70.     storage_type = Rheader.storage_type;
  71.     switch (storage_type)
  72.         {
  73.     case st_gray:
  74.     case st_vlt:
  75.         nchunks = 1;
  76.         break;
  77.     case st_rgb:
  78.     case st_hsv:
  79.     case st_cmy:
  80.         nchunks = 3;
  81.         break;
  82.     case st_cmyk:
  83.         nchunks = 4;
  84.         break;
  85.         }
  86.     /* read in resolution of picture */
  87.     dpi = Rheader.resolution;
  88.     if (dpi == 0)
  89.         dpi = 72; /* for old files... */
  90.     /* set the uncompressed flag */
  91.     uncompressed = (Rheader.flags&hf_uncompressed) != 0;
  92.     /* set the transfer function */
  93.     transfer_table = (unsigned char *)NewPtr(nsamples);
  94.     if (transfer_table == null)
  95.         goto no_mem2;
  96.     if (Rheader.flags&hf_transfer)
  97.         getbytes(&readparm, transfer_table, (long)nsamples);
  98.     else
  99.         {
  100.         /* assume normalized transfer */
  101.         for (i = 0; i < nsamples; i++)
  102.             transfer_table[i] = i;
  103.         }
  104.     /* convert transfer table for number of samples desired */
  105.     if (desired_nsamples != nsamples)
  106.         {
  107.         for (i = 0; i < nsamples; i++)
  108.             {
  109.             j = transfer_table[i];
  110.             j = ((long)desired_nsamples*(long)j + nsamples/2) / nsamples;
  111.             transfer_table[i] = j;
  112.             }
  113.         }
  114.     /* evaluate the size of one chunk */
  115.     lsize = (long)nrows * (long)ncols;
  116.     /* allocate all chunks at once */
  117.     body[0] = (unsigned char *)NewPtr(lsize * (long)nchunks);
  118.     if (body[0] == null)
  119.         goto no_mem1;
  120.     /* update all other chunk pointers */
  121.     for (i = 1; i < nchunks; i++)
  122.         body[i] = body[i-1] + lsize;
  123.     /* determine the size of a flat scan line (largest record coming in from data) */
  124.     flat = (4 + ncols) & ~1;
  125.     /* allocate the decoding buffer plus some slop */
  126.     huffbuff = (short *)NewPtr((long)flat + 16);
  127.     if (huffbuff == null)
  128.         {
  129.         DisposPtr(body[0]);
  130.         body[0] = null;
  131.     no_mem1:
  132.         DisposPtr(transfer_table);
  133.     no_mem2:
  134.         term_io();
  135.         /* close the file */
  136.         PBClose(&readparm, false);
  137.         return(1); /* not enough memory available */
  138.         }
  139.     /* load up the chunks now */
  140.     for (i = 0; i < nchunks; i++)
  141.         {
  142.         /* position to the header */
  143.         pos_read(&readparm, Rheader.chunks[i]);
  144.         /* do compressed or uncompressed readin */
  145.         if (uncompressed)
  146.             getbytes(&readparm, body[i], lsize);
  147.         else
  148.             COMPinput(&readparm, huffbuff, i);
  149.         }
  150.     /* free the decode buffer */
  151.     DisposPtr(huffbuff);
  152.     /* free I/O buffer */
  153.     term_io();
  154. clodraw:
  155.     /* close the file */
  156.     PBClose(&readparm, false);
  157.     return(0); /* file opened successfully */
  158.     }
  159.